package com.hk.web.controller.api;

import com.hk.commons.JsonResult;
import com.hk.commons.ProcessAPI;
import com.hk.commons.enums.ArticleOnTopType;
import com.hk.commons.enums.ArticleStatus;
import com.hk.commons.enums.ArticleType;
import com.hk.web.controller.Routes;
import com.hk.web.domain.entity.Article;
import com.hk.web.domain.lifeCycle.repository.ArticleRepository;
import com.hk.web.dto.QueryPagingDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.hk.commons.domain.service.DateFormatService.*;

/**
 * Created by Antinomy on 16/11/21.
 */
@RestController
public class PCFacadeAPIController extends APIController {

    private Logger logger = LoggerFactory.getLogger(PCFacadeAPIController.class);
    public static final String DYNAMIC = "DYNAMIC";

    @Autowired
    private ArticleRepository articleRepo;

    @RequestMapping(value = Routes.FLASHVIEW_PC_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listFlashview(HttpServletRequest request) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<Article> articles = articleRepo.findByTypeAndStatus(ArticleType.FLASHVIEW_PC, ArticleStatus.PUBLISHED);
            List result = renderFlashview(articles);

            jsonResult.result = result;
            jsonResult.size = Long.valueOf(result.size());
        };

        return api.process(request);
    }

    @RequestMapping(value = Routes.FLASHVIEW_PC_PROD_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listFlashview_Prod(HttpServletRequest request) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<Article> articles = articleRepo.findByTypeAndStatus(ArticleType.FLASHVIEW_PC_PRODUCT, ArticleStatus.PUBLISHED);
            List result = renderFlashview(articles);

            jsonResult.result = result;
            jsonResult.size = Long.valueOf(result.size());
        };

        return api.process(request);
    }

    @RequestMapping(value = Routes.COMPANY_DEVELOPMENT_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listCompanyDevelopment(HttpServletRequest request) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<Article> articles = articleRepo.findByTypeAndStatus(ArticleType.COMPANY_DEVELOPMENT, ArticleStatus.PUBLISHED);
            List result = renderCompanyDevelopment(articles);

            jsonResult.result = result;
            jsonResult.size = Long.valueOf(result.size());
        };

        return api.process(request);
    }

    @RequestMapping(value = Routes.COMPANY_GLORIES_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listCompanyGlories(HttpServletRequest request) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<Article> articles = articleRepo.findByTypeAndStatus(ArticleType.COMPANY_GLORIES, ArticleStatus.PUBLISHED);
            List result = renderCompanyGlories(articles);

            jsonResult.result = result;
            jsonResult.size = Long.valueOf(result.size());
        };

        return api.process(request);
    }

    @RequestMapping(value = Routes.COMPANY_LEADER_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listCompanyLeader(HttpServletRequest request) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<Article> articles = articleRepo.findByTypeAndStatus(ArticleType.COMPANY_LEADER, ArticleStatus.PUBLISHED);
            List result = renderCompanyLeader(articles);

            jsonResult.result = result;
            jsonResult.size = Long.valueOf(result.size());
        };

        return api.process(request);
    }

    @RequestMapping(value = Routes.COMPANY_TEAM_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listCompanyTeam(HttpServletRequest request) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<Article> articles = articleRepo.findByTypeAndStatusOrderByDisplayOrderAsc(ArticleType.COMPANY_TEAM, ArticleStatus.PUBLISHED);
            List result = renderCompanyTeam(articles);

            jsonResult.result = result;
            jsonResult.size = Long.valueOf(result.size());
        };

        return api.process(request);
    }

    @RequestMapping(value = Routes.COMPANY_JOBS_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listCompanyJobs(HttpServletRequest request) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<Article> articles = articleRepo.findByTypeAndStatus(ArticleType.COMPANY_JOBS, ArticleStatus.PUBLISHED);
            List result = renderCompanyJobs(articles);

            jsonResult.result = result;
            jsonResult.size = Long.valueOf(result.size());
        };

        return api.process(request);
    }

    @RequestMapping(value = Routes.COMPANY_WEEKLY_VIEW_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listCompanyWeeklyView(HttpServletRequest request) {

        ProcessAPI api = (jsonResult, validate) -> {
            List<Article> articles = articleRepo.findByTypeAndStatus(ArticleType.WEEKLY_VIEW, ArticleStatus.PUBLISHED);
            List result = renderCompanyWeeklyView(articles);

            jsonResult.result = result;
            jsonResult.size = Long.valueOf(result.size());
        };

        return api.process(request);
    }

    @RequestMapping(value = Routes.ARTICLE_TOP_LIST, method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listTop(HttpServletRequest request, QueryPagingDto pagingDto, @PathVariable String type) {

        ProcessAPI api = (jsonResult, validate) -> {
            String typeTxt = validate.checkAndPrepare("type", type);
            validate.check("curPage", pagingDto.getCurPage());
            validate.check("pageSize", pagingDto.getPageSize());

            List<ArticleType> typeList = getTypeList(type);

            Pageable pageable = getPage(pagingDto);
            Page<Article> articles = articleRepo.findByTypeInAndStatusAndOnTopOrderByEventDateDesc(typeList, ArticleStatus.PUBLISHED, ArticleOnTopType.ON_TOP, pageable);
            List result = renderTop(articles);

            jsonResult.result = result;
            jsonResult.size = articles.getTotalElements();
        };

        return api.process(request);
    }

    private List<ArticleType> getTypeList(String type) {
        List<ArticleType> result = new ArrayList<>();

        if(type.endsWith(DYNAMIC)){
            result.add(ArticleType.FOCUS_VIEW);
            result.add(ArticleType.WEALTH_REPORT);
            result.add(ArticleType.WEALTH_SCHOOL);
            return result;
        }

        ArticleType articleTypetype = ArticleType.valueOf(type);
        result.add(articleTypetype);

        return result;
    }

    private List renderFlashview(List<Article> articles) {
        ArrayList<HashMap> result = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();
            item.put("imgHref", article.getImageUrl());
            item.put("href", article.getOutSideUrl());
            result.add(item);
        }

        return result;
    }

    private List renderCompanyDevelopment(List<Article> articles) {
        ArrayList<HashMap> result = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();
            item.put("time", toMonth(article.getEventDate()));
            item.put("content", article.getContent());
            result.add(item);
        }

        return result;
    }

    private List renderCompanyGlories(List<Article> articles) {
        ArrayList<HashMap> result = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();
            item.put("title", article.getTitle());
            item.put("imgSrc", article.getImageUrl());
            result.add(item);
        }

        return result;
    }

    private List renderCompanyLeader(List<Article> articles) {
        ArrayList<HashMap> result = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();
            item.put("name", article.getTitle());
            item.put("position", article.getSummary());
            item.put("content", article.getContent());
            item.put("mainImg", article.getImageUrl());
            result.add(item);
        }

        return result;
    }

    private List renderCompanyTeam(List<Article> articles) {
        ArrayList<HashMap> result = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();
            item.put("name", article.getTitle());
            item.put("content", article.getSummary());
            item.put("backContent", article.getContent());
            item.put("reImgSrc", article.getImageUrl());
            result.add(item);
        }

        return result;
    }

    private List renderCompanyJobs(List<Article> articles) {
        ArrayList<HashMap> result = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();
            item.put("name", article.getTitle());
            item.put("content", article.getContent());
            result.add(item);
        }

        return result;
    }

    private List renderCompanyWeeklyView(List<Article> articles) {
        ArrayList<HashMap> result = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();
            item.put("title", article.getTitle());
            item.put("time", toDay(article.getEventDate()));
            item.put("imgUrl", article.getImageUrl());
            item.put("url", article.getOutSideUrl());
            result.add(item);
        }

        return result;
    }

    private List renderTop(Page<Article> articles) {
        ArrayList<HashMap> result = new ArrayList<>();

        for (Article article : articles) {
            HashMap item = new HashMap();
            item.put("id", article.getUuid());
            item.put("title", article.getTitle());
            item.put("ctsStr", toDay(article.getEventDate()));
            item.put("picAddress", article.getImageUrl());
            item.put("summary", article.getSummary());
            item.put("clickCounts", article.getClickCounts());
            result.add(item);
        }

        return result;
    }
}
